﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMPLib;
using System.Collections.ObjectModel;
using System.Reflection;

namespace MyWindowsMediaPlayer_V2
{
    public class CollectionManager
    {
        public System.Windows.Controls.MediaElement mediaInstance;
        public ObservableCollection<Media> mediaList;
        public WindowsMediaPlayer          wmp;
        public int                         currentMedia;

        public CollectionManager()
        {
            mediaList = new ObservableCollection<Media>();
            wmp = new WindowsMediaPlayer();
            currentMedia = 0;
        }
        public void removeAll_Items()
        {
            for (int i = (mediaList.Count - 1); i >= 0; --i)
            {
                if (i == currentMedia)
                    mediaList[currentMedia].stop_Media();
                mediaList.RemoveAt(i);
            }
        }
        public void removeSelected_media(List<int> indexes)
        {
            foreach (int index in indexes)
            {
                if (index >= 0 && index < mediaList.Count)
                {
                    if (index == currentMedia)
                        mediaList[index].stop_Media();
                    mediaList.RemoveAt(index);
                }
            }
        }
        public void addMediaTo_List(ObservableCollection<Media> from)
        {
            if (mediaList.Count > 0)
                removeAll_Items();
            foreach (Media media in from)
                mediaList.Add(media);
        }
        public void addMediaTo_List(string[] paths)
        {
            foreach (string path in paths)
            {
                Type mediaType = getMedia_Type(path);

                if (mediaType != null)
                {
                    MethodInfo method = this.GetType().GetMethod("Create_Media");
                    MethodInfo generic = method.MakeGenericMethod(mediaType);
                    var media = generic.Invoke(this, null) as Media;
                    media.setNameFrom_Path(path);
                    media.setWmp(wmp.newMedia(path));
                    media.setMediaInstance(mediaInstance);
                    mediaList.Add(media);
                }
            }
        }
        public T Create_Media<T>()
          where T : Media, new()
        {
            return new T();
        }

/*
**      GETTERS
*/
        public Type getMedia_Type(string path)
        {
            string type = path.Split(new Char[] { '.' }).Last();

            if (type == "jpg" || type == "png")
                return (typeof(Pics));
            else if (type == "avi" || type == "wmv" || type == "mp4" || type == "flv" || type == "mkv")
                return (typeof(Video));
            else if (type == "mp3")
                return (typeof(Music));
            return (null);
        }
        public void getSelected_Media(int pos)
        {
            currentMedia = pos;
            mediaList[currentMedia].play_Media();
        }
    }
}
